let MS = require('./ms')
const setHeader = (config) => {
    let header = {
        'X-MS-ApplicationId': config.applicationId,
        'X-MS-REST-API-KEY': config.applicationKey,
        'X-MS-APP-ID': config.appid
    }
    return header
}

const wxUploadFile = (filePath) => {
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: MS._config.host + MS._config.parameters.FILE.UPLOAD_FILE.url,
            filePath: filePath,
            name: 'file',
            header: setHeader(MS._config),
            success:(res) => {
                if (res.statusCode === 200) {
                    var data = JSON.parse(res.data)
                    if(data.success) {
                        resolve(data.data)
                    } else {
                        reject(data.message)
                    }
                } else {
                    reject(res)
                }
            },
            fail: err => {
                console.log(err)
                reject(err)
            }
        })
    })
}

module.exports = wxUploadFile